-- system for capturing keys.
local priority = 1

EntitySystem.updatesystem("keyInput", priority, function(dt) 
    for e in pairs(EntitySystem.query("controlled")) do
      --update inputs
      xInput = checkOpposingKeys(e, "a", "d")
      yInput = checkOpposingKeys(e, "w", "s")
      
      --update direction
      if xInput == "a" then
        e.motion.dir.x = -1
      elseif xInput == "d" then
        e.motion.dir.x = 1
      else
        e.motion.dir.x = 0
      end
      
      if yInput == "w" then
        e.motion.dir.y = -1
      elseif yInput == "s" then
        e.motion.dir.y = 1
      else
        e.motion.dir.y = 0
      end
      
      e.motion.dir
      
    end
end)

--returns the pressed key,
function checkOpposingKeys(entity,keyA, keyB)
  local domKey
  
  if love.keyboard.isDown(keyA) then
    domKey = keyA
    entity.inputDirection.up = true
    entity.inputDirection.down = false
  elseif love.keyboard.isDown(keyB) then
    domKey = keyB
    entity.inputDirection.up = false
    entity.inputDirection.down = true
  else
    entity.inputDirection.up = false
    entity.inputDirection.down = false
  end
  
  return domKey
end
